<?php
 
class TechBlogLoginValidator extends sfValidator
{    
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context);
 
    // set defaults
    $this->setParameter('login_error', 'Invalid input');
 
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }
 
  public function execute(&$value, &$error)
  {
	
	
	
	
    $password_param = $this->getParameter('password');
    $password = $this->getContext()->getRequest()->getParameter($password_param);
 
    $login = $value;
 
    $c = new Criteria();
    $c->add(UserPeer::EMAIL, $login);
    $user = UserPeer::doSelectOne($c);

	


    // nickname exists?
    if ($user)
    {
	
			
	
	
      // password is OK?
      if (sha1($user->getSalt().$password) == $user->getPasswordSha1())
      {
			
		
		
	 
		if ($this->getContext()->getRequest()->getParameter('remember_me') == '1') {
			
		   
				//$value = base64_encode(serialize(array($user->getPasswordSha1(), $user->getId())));	
				$cookievalue = base64_encode(serialize(array($user->getPasswordSha1(), $user->getId())));
				//$cookievalue = 'ewfwe';
				//$this->getContext()->getResponse()->setCookie('devunityXXX', 'test', time(), 'devunitydev.com');
			    //sfContext::getInstance()->getResponse()->setCookie('devunityuid', $cookievalue, time()+60*60*24*15, 'devunity.com');
		    	//sfContext::getInstance()->getResponse()->setCookie('devunityXXX', 'test', time(), 'devunitydev.com');
				setcookie('techbloguid', $cookievalue, time()+(60*60*24*365),'/');
			
		}
	
	
        $this->getContext()->getUser()->signIn($user);
        return true;
      }
    }
 
    $error = $this->getParameter('login_error');
    return false;
  	}
	
}
